package com.llkj.qingnianguoxue.circle;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.jm.chat.activity.ChatActivity;
import com.jm.chat.application.JGApplication;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.WorksAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.MemberDetailBean;
import com.llkj.qingnianguoxue.bean.WorkListBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.mine.MyFriendsActivity;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.llkj.qingnianguoxue.circle.CircleMemberActivity.REQUEST_CODE_ADD_FRIEND;

public class CircleOwnerDetailActivity extends BaseActivity {
    public static final String IS_REPORT = "is_report";
    @BindView(R.id.ivHead)
    protected ImageView mIvHead;//圈住头像
    @BindView(R.id.tvName)
    protected TextView mTvName;//圈住名字
    @BindView(R.id.tvAge)
    protected TextView mTvAge;//年龄
    @BindView(R.id.tvPersonalitySignature)
    protected TextView mTvSignature;//签名
    @BindView(R.id.rlWorks)
    protected RecyclerView mRlWorks;//作品
    @BindView(R.id.tvAddFriend)
    protected TextView mTvAddFriend;//添加好友、发消息
    private WorksAdapter mWorkAdapter;
    private List<WorkListBean> mWorksData;
    private String mMemberId,mUserName,mChatId;//成员id,用户名，聊天id
    private String mFriendState;//好友状态,1好友
    private String isShowBottomBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_owner_detail);
        setTitle("资料", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_report_member);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        mMemberId = intent.getStringExtra("ownerId");

//        boolean isReport = intent.getBooleanExtra(IS_REPORT,false);
//        if(!isReport){//显示举报
//            ivRight.setVisibility(View.GONE);
//        }
        isShowBottomBtn = intent.getStringExtra("BottomBtn");

        initData();

        loadWaitDialog();
        HttpMethod.circleDetailMember(this,this,mMemberId);
    }

    private void initData(){
        mRlWorks.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
        mWorksData = new ArrayList<>();
        mWorkAdapter = new WorksAdapter(R.layout.item_owner_work,mWorksData);
        mRlWorks.setAdapter(mWorkAdapter);

        mRlWorks.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }
        });
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        if(ivRight.getVisibility()==View.VISIBLE){
            backgroundAlpha(0.8f);
            View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_report, null);
            TextView tvReport = (TextView) contentView.findViewById(R.id.tvReport);
            final PopupWindow popupWindow = new PopupWindow(contentView, DisplayUtil.dip2px(this,150), ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            // 设置好参数之后再show
//        popupWindow.showAsDropDown(contentView);
            View windowContentViewRoot = contentView;
            int windowPos[] = DisplayUtil.calculatePopWindowPos(ivRight, windowContentViewRoot);
            int xOff = DisplayUtil.dip2px(this,130);// 可以自己调整偏移
            windowPos[0] -= xOff;
            windowPos[1] += DisplayUtil.dip2px(this,10);
            popupWindow.showAtLocation(ivRight, Gravity.TOP| Gravity.LEFT, windowPos[0], windowPos[1]);

            tvReport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                    Intent intent = getIntent();
                    Bundle bundle = new Bundle();
                    bundle.putString("id",mMemberId);
                    bundle.putString("circleId",intent.getStringExtra("circleId"));
                    CircleOwnerDetailActivity.this.startActivity(PublicReportActivity.class,bundle);
                }
            });
            // windowContentViewRoot是根布局View
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    backgroundAlpha(1);
                }
            });
        }

    }
    // 设置屏幕透明度
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0~1.0
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.getWindow().setAttributes(lp); //act 是上下文context
    }

    @OnClick({R.id.rlWork, R.id.tvAddFriend})
    public void onClick(View v){
        Bundle bundle = null;
        switch (v.getId()){
            case R.id.rlWork://查看作品
                bundle = new Bundle();
                bundle.putString("uid",mMemberId);
                startActivity(WorkListActivity.class,bundle);
                break;
            case R.id.tvAddFriend://申请好友、聊天
                if("1".equals(mFriendState)){
                    if(!StringUtils.isEmpty(mChatId)){
                        MyChatActivity.startSingleChat(this,mUserName,mChatId,mMemberId);
                    }else{
                        ToastUtil.makeLongText(this,"对不起，暂时不能聊天");
                    }
                    return;
                }else if("4".equals(mFriendState)){//添加好友
//                    ToastUtil.makeLongText(this,getString(R.string.friend_request_again));
                    bundle = new Bundle();
                    bundle.putString("uid",mMemberId);
                    startActivityForResult(AppyVerificationActivity.class,bundle,REQUEST_CODE_ADD_FRIEND);
                }
//
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK && requestCode == REQUEST_CODE_ADD_FRIEND){
            mFriendState = "1";
            mTvAddFriend.setText("发消息");
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_CIRCLE_DETAIL_MEMBER) {//成员详情
            MemberDetailBean memberDetailBean = GsonUtil.GsonToBean(data,MemberDetailBean.class);
            if(memberDetailBean!=null){
                if(memberDetailBean.getUser()!=null){
                    MemberDetailBean.UserBean userBean = memberDetailBean.getUser();
                    ImageLoadingUtil.loadingCircleBorderImg(mIvHead,userBean.getUserpic(), R.color.transparent);
                    mUserName = userBean.getUsername();
                    mChatId = userBean.getJg_id();
                    mTvName.setText(userBean.getUsername());
                    mTvAge.setText(userBean.getAge());
                    mTvSignature.setText(userBean.getSignature());

                    mFriendState = userBean.getIs_friend();
                    if("1".equals(mFriendState)){//好友
                        mTvAddFriend.setText("发消息");
                    }else if("4".equals(mFriendState)){
                        mTvAddFriend.setText("添加好友");
                    }else{
                        mTvAddFriend.setText("待确认");
                    }

                    String myChatId = MyApplication.getInstance().sputil.getString(ConstantUserInfo.CHAT_LOGIN_ID);
                    if("1".equals(isShowBottomBtn)){
                        mTvAddFriend.setVisibility(View.GONE);
                    }else{
                        if(myChatId.equals(mChatId) || StringUtils.isEmpty(mChatId)){
                            mTvAddFriend.setVisibility(View.GONE);
                        }else{
                            mTvAddFriend.setVisibility(View.VISIBLE);
                        }
                    }
                }
                if( memberDetailBean.getSing()!=null){
                    int size = memberDetailBean.getSing().size();
                    size = size>3?3:size;
                    mWorksData.addAll(memberDetailBean.getSing().subList(0,size));
                    mWorkAdapter.notifyDataSetChanged();
                }
            }
        }
    }
}
